package com.daos;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.springframework.stereotype.Service;

import com.model.Employee;
import com.model.Task;
@Service
public class TaskDao extends AbstractDao<Task>{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -1531452266677844092L;

	@SuppressWarnings("unchecked")
	public List<Task>getAllTasks()
	{
		return  hibernateTemplate.find("from Tasks t where t.visible = true");
	}

	public Task getTaskById(long id) {
		Task task =  (Task) hibernateTemplate.find("from Tasks t where t.id = ? and t.visible = true",id).get(0);
		return task != null ?  task: null;
	}

	public List<Task> getTasksForEmployee(int employeeId) {
		
		@SuppressWarnings("unchecked")
		List<Task> tasks =   hibernateTemplate.find("from Tasks t where t.employee.id = ? and t.visible = true order by t.finishDate asc ",employeeId);
		return tasks != null ?  tasks: null;
	}

	public List<Task> getTasksByActivityId(long id) {
		@SuppressWarnings("unchecked")
		List<Task> tasks =   hibernateTemplate.find("from Tasks t where t.activity.id = ? and t.visible = true and t.activity.visible = true order by t.name asc",id);
		return tasks != null ?  new ArrayList<Task>(tasks): null;
	}

	public List<Task> getTasksForEmployeeByUsername(String username) {
		@SuppressWarnings("unchecked")
		List<Task> tasks =   hibernateTemplate.find("from Tasks t where t.employee.username = ? and t.visible = true",username);
		return tasks != null ?  new ArrayList<Task>(tasks): null;
	}


	public List<Task> getFinishedTasks(long id) {
		@SuppressWarnings("unchecked")
		List<Task> tasks =   hibernateTemplate.find("from Tasks t where t.activity.id = ? and t.progress = 100 and t.visible = true and t.activity.visible = true order by t.name asc",id);
		return tasks != null ?  new ArrayList<Task>(tasks): null;
	}

	public List<Task> getCurrentTasks(long id) {
		@SuppressWarnings("unchecked")
		List<Task> tasks =   hibernateTemplate.find("from Tasks t where t.activity.id = ? and t.progress != 100 and t.visible = true order by t.name asc",id);
		return tasks != null ?  new ArrayList<Task>(tasks): null;
	}

	public  ArrayList<Task> getTasksByActivityIdForEmployee(long id, Employee loggedInUser) {
		@SuppressWarnings("unchecked")
		List<Task> tasks =   hibernateTemplate.find("from Tasks t where t.activity.id = ? and t.progress != 100 and t.visible = true and t.employee.username = ? order by t.name asc",id,loggedInUser.getUsername());
		return tasks != null ?  new ArrayList<Task>(tasks): null;
		
	}

	public List<Task> getTasksToBeApprovedOrderByDeadLine() {
		@SuppressWarnings("unchecked")
		List<Task> tasks =   hibernateTemplate.find("select t from Tasks t where t.visible = true and t.activity.visible = true and t.progress = 75 order by t.finishDate asc");
		return tasks != null ?  tasks: null;
	}
	
	public List<Task> getAllTasksWithDueDates() {
		@SuppressWarnings("unchecked")
		List<Task> tasks =   hibernateTemplate.find("select distinct t from Tasks t where t.visible = true and t.activity.visible = true and t.progress < 100 " +
				"and t.finishDate <= ? order by t.finishDate asc",Calendar.getInstance().getTime());
		return tasks != null ?  tasks: null;
	}

	
}
